#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <iostream>
#include <string.h>
using namespace std;

void dfsFolder( string folderPath, int depth, string extension )
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    if((dp = opendir(folderPath.c_str())) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", folderPath.c_str());
        return;
    }
    //chdir(folderPath.c_str());
    while((entry = readdir(dp)) != NULL) {
        //printf("%s\n", entry->d_name);
        string filename = folderPath + string("/") + entry->d_name;
        lstat(filename.c_str(),&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            if(strcmp(".",entry->d_name) == 0 ||
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("d%*s%s/\n",depth,"", filename.c_str());
            //printf("%*s%s/\n",depth,"",entry->d_name);
            dfsFolder( filename,depth+4, extension);
        } else if (S_ISREG(statbuf.st_mode)) {
            char *pos = strrchr( entry->d_name, '.' );
            if( !pos || 0 != strcmp( pos+1, extension.c_str() )){
              continue;
            }

            printf("f%*s%s\n", depth,"", filename.c_str());
            //printf("%*s%s\n",depth,"",entry->d_name);
        }
    }
    //chdir("..");
    closedir(dp);
}

int main(){
  dfsFolder("/home/yong/Works/AlgorithmResearch/GenerateCode",0, "sample");
}
